package com.umakr.ax.app.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.mapper.SeekHelpInfoMapper;
import com.umakr.ax.common.model.Msg;
import com.umakr.ax.common.model.SeekHelpInfo;
import com.umakr.ax.common.service.MsgService;
import com.umakr.ax.common.service.RadioStationFollowerService;
import com.umakr.ax.common.service.SeekHelpInfoService;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class SeekHelpInfoWebService extends BaseService<SeekHelpInfoMapper, SeekHelpInfo> {
    @Autowired
    SeekHelpInfoService seekHelpInfoService;
    @Autowired
    MsgService msgService;
    @Autowired
    RadioStationFollowerService radioStationFollowerService;
    @Autowired
    Environment env;


    public Object listRsPubSeekHelpInfo(Long rsId,String searchText) {
        Page<?> page = baseMapper.listRsPubSeekHelpInfo(rsId,searchText);
        return page;
    }

    /**
     * 导播在平台发布的消息，状态设置为：1 导播已审
     * @param seekHelpInfo
     * @return
     */
    public Object saveByRs(SeekHelpInfo seekHelpInfo,String userName) {
        String aliUrl = env.getProperty("oss.img.path");
        int errorCode = 0 ;
        String msg = "发布失败";
        Map<String,Object> data = Maps.newLinkedHashMap();
        Date createTime = new Date();

        Long id ;
        Msg msgInfo ;
        seekHelpInfo.setCreateTime(createTime);
        seekHelpInfo.setPhotoInfo(seekHelpInfo.getPhotoInfo().replace(aliUrl,""));
        if(seekHelpInfo.getId() == null){
            seekHelpInfo.setReviewStatus(1);
            baseMapper.save(seekHelpInfo);
            id = seekHelpInfo.getId();
            msgInfo = new Msg();
        }else{
            seekHelpInfo.setReviewStatus(1);
            baseMapper.update(seekHelpInfo);
            id = seekHelpInfo.getId();
            EntityWrapper<Msg> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("help_id",id);
            msgInfo = msgService.selectOne(entityWrapper);
            if( msgInfo == null ) {
                msgInfo = new Msg();
            }
        }

        seekHelpInfo = queryById(id);

        msgInfo.setMsgTime(createTime);
        //求助类型: 1 寻人 2 寻物 3 车辆求助 4 寻找失主 5 其他
        //消息类型:  0 互动  1 寻人 2 寻物 3 车辆求助 4 寻找失主 5 其他 需要根据求助类型判断
        msgInfo.setMsgType(seekHelpInfo.getSeekType());
        //消息内容类型  0、text  1、语音   2、图片  3、位置,
        //此处导播发送的只有文字，没有语音
        msgInfo.setMsgContentType(0);
        //附件地址已经更名为：消息内容  此处存放的是求助信息的标题
        msgInfo.setMsgAttachmentAddr(seekHelpInfo.getSeekTitle());
        msgInfo.setUserId(seekHelpInfo.getUserId());
        msgInfo.setRsId(seekHelpInfo.getRsId());
        //消息方向 0 用户到电台 1 电台到用户 2 导播到主播
        msgInfo.setMsgDirect(2);
        //消息状态设置为1 导播已审
        msgInfo.setStatus(1);
        //设置当前登录用户的ID和姓名以及审核时间
        msgInfo.setAuditUserId(seekHelpInfo.getUserId());
        msgInfo.setAuditUserName(userName);
        msgInfo.setAuditTime(createTime);

        msgInfo.setRadioLen(seekHelpInfo.getRadioLen());
        msgInfo.setPositionCoor(seekHelpInfo.getAddrCoordinate());
        msgInfo.setPositionName(seekHelpInfo.getAddrStr());
        //消息来源：0 微信 1 app  2 平台发布
        msgInfo.setMessageFrom(2);
        msgInfo.setHelpId(seekHelpInfo.getId());
        msgInfo.setDelStatus(0);
        msgService.update(msgInfo);
        msg = "发布成功";
        errorCode = 1;

        return  resultMap(data,errorCode,msg);
    }


    public Object delSeekHelpInfo(Long id, Integer delStatus) {
       return seekHelpInfoService.delSeekHelpInfo(id,delStatus);
    }


    public Object updateSeekHelpInfoDealStatus(Long id, Integer dealStatus) {
        return seekHelpInfoService.updateSeekHelpInfoDealStatus(id,dealStatus);
    }

    public Object updateSeekHelpInfoPushStatus(Long id, Integer pushStatus) {
        return seekHelpInfoService.updateSeekHelpInfoPushStatus(id,pushStatus);
    }


    public Object getSeekHelpInfoById(Long seekHelpId) {
        String aliUrl = env.getProperty("oss.img.path");
        SeekHelpInfo info = queryById(seekHelpId);
        Map<String,Object> map = Maps.newHashMap();
        map.put("seekType",info.getSeekType().toString());
        map.put("seekerName",info.getSeekerName());
        map.put("seekTitle",info.getSeekTitle());
        map.put("seekDes",info.getSeekDes());
        map.put("contacts",info.getContacts());
        map.put("contactsTell",info.getContactsTell());
        map.put("id",info.getId());
        map.put("rsId",info.getRsId());
        map.put("photoInfo", FormatPhotoUrlUtil.formatStringArray(aliUrl,info.getPhotoInfo()));

        JSONObject addrJson = JSONObject.parseObject(info.getAddrCoordinate());
        map.put("lng",addrJson.get("lng"));
        map.put("lat",addrJson.get("lat"));
        map.put("areaAddr",info.getAddrStr());

        return resultMap(map);
    }

}
